package com.chaoxuan.uebt.activity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chaoxuan.uebt.R;
import com.chaoxuan.uebt.model.User;
import com.chaoxuan.uebt.utils.BaseSubscriber;
import com.chaoxuan.uebt.utils.Const;
import com.chaoxuan.uebt.utils.Loading;
import com.chaoxuan.uebt.utils.RetrofitUtils;
import com.chaoxuan.uebt.utils.CacheUtils;
import com.chaoxuan.uebt.utils.ToastUtils;

public class UserModifyPwdActivity extends BaseActivity implements View.OnClickListener,TextWatcher {

    EditText editOldPwd,editNewPwd,editRePwd;
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_modify_pwd);
        editOldPwd=(EditText)findViewById(R.id.edit_old_pwd);
        editNewPwd=(EditText)findViewById(R.id.edit_new_pwd);
        editRePwd=(EditText)findViewById(R.id.edit_re_pwd);
        button=(Button) findViewById(R.id.btn_confirm);
        button.setOnClickListener(this);
        initTitle();
        editOldPwd.addTextChangedListener(this);
        editNewPwd.addTextChangedListener(this);
        editRePwd.addTextChangedListener(this);
        button.setSelected(true);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_confirm:
                if(check())return;
                User user= CacheUtils.getInstance(getBaseContext()).getUser();
                if(user==null)return;
                String oldPwd=editOldPwd.getText().toString();
                String newPwd=editNewPwd.getText().toString();
                final Loading loading=new Loading(this,R.style.dialog);
                loading.show();
                subscribe(RetrofitUtils.getAPIService().changePass(user.getToken(),oldPwd,newPwd, Const.DEVICE_TYPE),new BaseSubscriber<Object>(){
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        loading.dismiss();
                    }
                    @Override
                    public void onSuccess(Object obj) {
                        ToastUtils.showCustomToast("设置成功");
                    }
                });
                break;
        }
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable editable) {
        button.setSelected(check());
    }
    private boolean check(){
        String oldPwd=editOldPwd.getText().toString();
        String newPwd=editNewPwd.getText().toString();
        String rePwd=editRePwd.getText().toString();
        if(TextUtils.isEmpty(oldPwd)||TextUtils.isEmpty(newPwd)||(!newPwd.equals(rePwd)))return true;
        return false;
    }
}
